package com.cz.cloud.order.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cz.cloud.client.dto.OrderDTO;
import com.cz.cloud.order.entity.TOrderEntity;
import com.cz.cloud.order.mapper.TOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrderController {


    @Autowired
    private TOrderMapper orderMapper;

    @Value("${node}")
    private String node;

    @GetMapping("/{userId}")
    public List<OrderDTO> getOrdersByUserId(@PathVariable("userId") Long userId) {
        System.out.println("userId = " + userId);
        List<TOrderEntity> orderEntityList = orderMapper.selectList(new LambdaQueryWrapper<TOrderEntity>().eq(TOrderEntity::getUserId, userId));
        List<OrderDTO> orderDTOList = BeanUtil.copyToList(orderEntityList, OrderDTO.class);
        if(CollUtil.isNotEmpty(orderDTOList)){
            orderDTOList.forEach(orderDTO -> {
                orderDTO.setNode(node);
            });
        }
        return orderDTOList;
    }
}
